﻿Public Class UCQLKhachHang
    Private KhachHangBUS As KhachHangBUS
    Private KhachHangDTO As KhachHangDTO
    Dim connect As New connect()
    Private Sub UCQLKhacHang_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadKH()

    End Sub
    Private Sub LoadKH()
        dgvKhachHang.ClearSelection()
        KhachHangBUS = New KhachHangBUS
        Dim KhachHangList As New List(Of KhachHangDTO)
        KhachHangList = KhachHangBUS.GetData()
        dgvKhachHang.DataSource = KhachHangList
    End Sub
    Private Sub LamMoi()
        LoadKH()
        txtMaKH.Text = ""
        txtHoTen.Text = ""
        dtNgaySinh.Text = ""
        txtCMND.Text = ""
        txtEmail.Text = ""
        rdbNam.Checked = True
        rdbNu.Checked = False
        txtDienThoai.Text = ""
        txtDiaChi.Text = ""
        cbBiXoa.Checked = False
        btnCapNhat.Enabled = False
        btnXoa.Enabled = False
        btnThem.Enabled = True
        btnCapNhat.Enabled = False
        btnXoa.Enabled = False
        btnThem.Enabled = True

    End Sub
    Private Sub dgvKhachHang_SelectionChanged(sender As System.Object, e As System.EventArgs) Handles dgvKhachHang.SelectionChanged
        btnThem.Enabled = False
        btnCapNhat.Enabled = True
        btnXoa.Enabled = True
        txtMaKH.Text = dgvKhachHang.CurrentRow.Cells(0).Value.ToString()
        txtHoTen.Text = dgvKhachHang.CurrentRow.Cells(1).Value.ToString()
        dtNgaySinh.Text = dgvKhachHang.CurrentRow.Cells(2).Value.ToString()
        txtCMND.Text = dgvKhachHang.CurrentRow.Cells(3).Value.ToString()
        Dim gioiTinh As Boolean = Convert.ToBoolean(dgvKhachHang.CurrentRow.Cells(4).Value.ToString())
        If (gioiTinh = False) Then
            rdbNu.Checked = True
            rdbNam.Checked = False
        ElseIf (gioiTinh = True) Then
            rdbNam.Checked = True
            rdbNu.Checked = False
        End If
        txtDienThoai.Text = dgvKhachHang.CurrentRow.Cells(5).Value.ToString()
        txtEmail.Text = dgvKhachHang.CurrentRow.Cells(6).Value.ToString()
        txtDiaChi.Text = dgvKhachHang.CurrentRow.Cells(7).Value.ToString()
        cbBiXoa.Checked = Convert.ToBoolean(dgvKhachHang.CurrentRow.Cells(8).Value.ToString())
    End Sub

    Private Sub btnCapNhat_Click(sender As System.Object, e As System.EventArgs) Handles btnCapNhat.Click
        Dim maKH As String = txtMaKH.Text.Trim()
        Dim tenKH As String = txtHoTen.Text.Trim()
        Dim ngaySinh As String = dtNgaySinh.Text.Trim()
        Dim CMND As String = txtCMND.Text.Trim()
        Dim Email As String = txtEmail.Text.Trim()
        Dim diaChi As String = txtDiaChi.Text.Trim()
        Dim dienThoai As String = txtDienThoai.Text.Trim()
        Dim gioiTinh As String
        If (rdbNam.Checked) Then
            gioiTinh = rdbNam.Checked.ToString()
        Else
            gioiTinh = rdbNam.Checked.ToString()
        End If
        Dim biXoa As String = cbBiXoa.Checked.ToString()
        Dim sql As String = "update KHACHHANG set HOTEN=N'" + tenKH + "',NGAYSINH='" + ngaySinh + "',CMND='" + CMND + "',GIOITINH='" + gioiTinh + "',DIENTHOAI='" + dienThoai + "',EMAIL=N'" + Email + "',DIACHI=N'" + diaChi + "',BIXOA='" + biXoa + "' where MAKH='" + maKH + "'"
        connect.ExecSql(sql)
        MessageBox.Show("Lưu thành công!")
        LoadKH()
        LamMoi()
    End Sub

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        LamMoi()
        btnThem.Enabled = True
        btnCapNhat.Enabled = False
        btnXoa.Enabled = False
    End Sub

    Private Sub rdbNam_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdbNam.CheckedChanged
        If (rdbNam.Checked) Then
            rdbNu.Checked = False
        Else
            rdbNu.Checked = True
        End If
    End Sub

    Private Sub rdbNu_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdbNu.CheckedChanged
        If (rdbNu.Checked) Then
            rdbNam.Checked = False
        Else
            rdbNam.Checked = True
        End If
    End Sub
    Private Function TaoMaKH() As String
        Dim maKH As String

        Dim yy As Integer = DateTime.Today.Year Mod 100
        Dim mm As Integer = DateTime.Today.Month

        Dim maDau As String = yy.ToString() + mm.ToString()
        Dim dt As DataTable = connect.GetData("select * from KHACHHANG where MAKH like '" + maDau + "%'")
        Dim stt As Integer = dt.Rows.Count() + 1
        Dim str_stt As String = stt.ToString("000")

        maKH = maDau + str_stt

        Dim check As Boolean = False
        While (check = False)
            Dim dt2 As DataTable = connect.GetData("select * from KHACHHANG where MAKH = '" + maKH + "'")
            If (dt2.Rows.Count() > 0) Then
                stt = stt + 1
                str_stt = stt.ToString("000")
                maKH = maDau + str_stt
                Dim dt3 As DataTable = connect.GetData("select * from KHACHHANG where MAKH='" + maKH + "'")
                If (dt3.Rows.Count() > 0) Then
                    check = False
                Else
                    check = True
                End If
            Else
                check = True
            End If
        End While

        Return maKH
    End Function
    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        Dim maKH As String = TaoMaKH()
        Dim tenKH As String = txtHoTen.Text.Trim()
        Dim ngay As DateTime = dtNgaySinh.Value
        Dim ngaySinh As String = ngay.Year().ToString("0000") + "-" + ngay.Month().ToString("00") + "-" + ngay.Day().ToString("00")

        Dim CMND As String = txtCMND.Text.Trim()
        Dim Email As String = txtEmail.Text.Trim()
        Dim diaChi As String = txtDiaChi.Text.Trim()
        Dim dienThoai As String = txtDienThoai.Text.Trim()
        Dim gioiTinh As String
        If (rdbNam.Checked) Then
            gioiTinh = rdbNam.Checked.ToString()
        Else
            gioiTinh = rdbNam.Checked.ToString()
        End If
        Dim biXoa As String = cbBiXoa.Checked.ToString()
        Dim sql As String = "insert into KHACHHANG(MAKH,HOTEN,NGAYSINH,CMND,GIOITINH,DIENTHOAI,EMAIL,DIACHI,BIXOA) values('" + maKH + "',N'" + tenKH + "','" + ngaySinh + "','" + CMND + "','" + gioiTinh + "','" + dienThoai + "',N'" + Email + "',N'" + diaChi + "','" + biXoa + "')"
        connect.ExecSql(sql)
        LoadKH()
        LamMoi()
    End Sub

    Private Sub btnXoa_Click(sender As System.Object, e As System.EventArgs) Handles btnXoa.Click
        If (txtMaKH.Text <> "") Then
            Dim sql As String = "select * from TINDANG where MAKH='" + txtMaKH.Text + "'"
            Dim dt As New DataTable
            dt = connect.GetData(sql)
            If (dt.Rows.Count() > 0) Then
                Dim result = MessageBox.Show("Không thể xoá vì khách hàng này đã đăng tin.Bạn có muốn đánh dấu Xoá không.", "Thông báo", MessageBoxButtons.YesNo)
                If result = DialogResult.No Then
                    'Me.Close()
                ElseIf result = DialogResult.Yes Then
                    sql = "update KHACHHANG set BIXOA='True' where MAKH='" + txtMaKH.Text + "'"
                    connect.ExecSql(sql)
                    MessageBox.Show("Đánh dấu xoá thành công!")
                    LoadKH()
                    LamMoi()
                End If
            Else
                Dim sqldelete As String = "delete KHACHHANG where MAKH='" + txtMaKH.Text + "'"
                connect.ExecSql(sqldelete)
                MessageBox.Show("Xoá thành công!")
                LoadKH()
                LamMoi()
            End If
        End If
    End Sub
End Class
